<?php


namespace app\user\controller;

use think\facade\Request;
use think\facade\Session;
use app\user\model\Select as selectmodel;
use app\user\model\Student as studentmodel;
use app\user\model\Message as messagemodel;
use app\user\model\Notice as noticemodel;
use app\user\model\Order as ordermodel;
use app\user\model\Expert as expertmodel;
use app\user\model\Notify as notifymodel;
use app\user\model\Ucpaas;

use think\Controller;

class Expert extends Controller
{

    //检测session
    protected function logincheck()
    {
        if (Session::get('expertname')) {
            return true;
        } else {
            return false;
        }
    }

    //盐值加密函数
    public function salt($pwd)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $salt  = "ssjxyz";  //定义一个salt值，程序员规定下来的随机字符串
            $spwd  = $pwd . $salt;  //把密码和salt连接
            $mspwd = md5($spwd);  //执行MD5散列
            return $mspwd;  //返回散列
        }
    }

    //首页
    public function index()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("expert/index");
        }

    }

    //查看留言
    public function message()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $list  = messagemodel::where('towho', 'expert')->select();
            $count = sizeof($list);

            $where1['receive'] = Session::get('expertname');
            $where1['estatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);
            $this->assign([
                'count'  => $count,
                'xcount' => $ccount
            ]);
            return $this->fetch("expert/message");
        }

    }


    //公告
    public function notice()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $res = noticemodel::select();
            $this->assign([
                'notice' => $res,
            ]);
            return $this->fetch("student/notice");
        }
    }


    //消息通知
    public function notify()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $res = notifymodel::where('receive', Session::get('expertname'))->select();
            $this->assign([
                'notify' => $res,
            ]);
            return $this->fetch("expert/notify");
        }

    }

    //取消提示
    public function changenotify()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $status           = 0;
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            foreach ($list as $notify) {
                notifymodel::where('id', $notify['id'])->update(['estatus' => 2]);
                $status = 1;
            }
        }
        return $status;
    }


    //取消预约
    public function cancel()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $status     = 0;
                $data       = Request::post();
                $time       = date('Y-m-d H:i:s', time());
                $res        = ordermodel::destroy($data['id']);
                $notifydata = [
                    "write"     => Session::get('expertname'),
                    "receive"   => $data['sname'],
                    "sstatus"   => 1,
                    "eopera"    => 2,
                    "time"      => $time,
                    "ordertime" => $data['time']
                ];
                $notify     = notifymodel::insert($notifydata);
                if ($res) {
                    $status = 1;
                } else {
                    $status = 2;
                }
            }
        }
        return $status;
    }

    //检查消息通知
    public function checkmessage()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            return $count;
        }

    }

    //清除通知
    public function clear()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $status           = 0;
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 2;
            $list             = notifymodel::where($where)->select();
            foreach ($list as $notify) {
                notifymodel::destroy($notify['id']);
                //notifymodel::where('id', $notify['id'])->update(['estatus' => 3]);
                $status = 1;
            }
        }
        return $status;
    }

    //显示修改手机信息页面
    public function phone()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $info             = expertmodel::where('id', Session::get('expertid'))->find();
            $phone            = $info['phone'];
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign([
                'phone' => $phone,
                'count' => $count
            ]);
            return $this->fetch("expert/phone");
        }
    }


    //修改手机操作
    public function updatephone($phone)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $newdata = [
                    'id'    => Session::get('expertid'),
                    'phone' => $phone,
                ];
                $res     = expertmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }


    //若头像为空则设置默认图片
    public function checkphoto()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $spath = expertmodel::where('id', Session::get('expertid'))->value('photo');
            if (empty($spath)) {
                $path    = '..\..\photo\default.jpg';
                $newdata = [
                    'id'    => Session::get('expertid'),
                    'photo' => $path,
                ];
                $res     = expertmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            } else {
                $this->error('', '', '0');
            }
        }
    }

    //个人信息
    public function info()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $info             = expertmodel::where('id', Session::get('expertid'))->find();
            $photo            = $info['photo'];
            $id               = $info['id'];
            $person           = $info['person'];
            $sex              = $info['sex'];
            $level            = $info['level'];
            $mail             = $info['mail'];
            $age              = $info['age'];
            $address          = $info['address'];
            $phone            = $info['phone'];
            $list             = selectmodel::field('sex')->select();
            $list2            = selectmodel::field('level')->select();
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list1            = notifymodel::where($where)->select();
            $count            = sizeof($list1);
            $this->assign([
                'photo'     => $photo,
                'id'        => $id,
                'name'      => Session::get('expertname'),
                'sex'       => $sex,
                'mail'      => $mail,
                'depart'    => $level,
                'age'       => $age,
                'address'   => $address,
                'phone'     => $phone,
                'nation'    => $person,
                'info'      => $info,
                'sexlist'   => $list,
                'levellist' => $list2,
                'count'     => $count
            ]);
            return $this->fetch("expert/info");
        }

    }

    //获取更新后的信息
    public function getinfo($id)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $list         = selectmodel::field('sex')->select();
            $list2        = selectmodel::field('level')->select();
            $list3        = expertmodel::where('id', '=', $id)->find();
            $arr          = array();
            $arr['list']  = $list;
            $arr['list2'] = $list2;
            $arr['list3'] = $list3;
            return json($arr);
        }
    }


    //修改信息发送验证码
    public function sms($tel)
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $param                 = rand(1000, 9999);
            $options['accountsid'] = 'd1e5040273e564e43bc4289d756cbd0f';
            $options['token']      = 'd8b817ed9afe00f1feb9b8a99959cf13';
            $ucpaas                = new Ucpaas($options);
            $appid                 = "1afae4daf8df4e7ebba63d9c7c988dbe";
            $templateid            = "565858";
            $mobile                = $tel;
            $uid                   = $param;
            $result                = $ucpaas->SendSms($appid, $templateid, $param, $mobile, $uid);
            $arr                   = array();
            //$arr['result'] = $result;
            $arr['param'] = $param;
            return $result;
        }
    }


    //我的预约
    public function myorder()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['ename']  = Session::get('expertname');
            $where['status'] = 1;
            $list            = ordermodel::where($where)->select();
            $count           = sizeof($list);
            $list            = selectmodel::field('sex')->select();
            $list2           = selectmodel::field('depart')->select();
            $list3           = selectmodel::field('slevel')->select();
            $list4           = expertmodel::field('name')->select();
            $list5           = selectmodel::field('roomnum')->select();

            $where1['receive'] = Session::get('expertname');
            $where1['estatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);

            $this->assign([
                'count'       => $count,
                'sexlist'     => $list,
                'departlist'  => $list2,
                'slevellist'  => $list3,
                'enamelist'   => $list4,
                'roomnumlist' => $list5,
                'xcount'      => $ccount
            ]);
            return $this->fetch("expert/myorder");
        }
    }


    //已完成
    public function complete()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['ename']  = Session::get('expertname');
            $where['status'] = 2;
            $list            = ordermodel::where($where)->select();
            $count           = sizeof($list);

            $where1['receive'] = Session::get('expertname');
            $where1['estatus'] = 1;
            $slist             = notifymodel::where($where1)->select();
            $ccount            = sizeof($slist);

            $this->assign([
                'count'  => $count,
                'xcount' => $ccount
            ]);

            return $this->fetch("expert/complete");
        }
    }


    //显示进行中的预约
    public function orderjson()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $page            = input("get.page") ? input("get.page") : 1;
            $page            = intval($page);
            $limit           = input("get.limit") ? input("get.limit") : 1;
            $limit           = intval($limit);
            $start           = $limit * ($page - 1);
            $where['ename']  = Session::get('expertname');
            $where['status'] = 1;
            $list            = ordermodel::where($where)->limit($start, $limit)->select();
            $count           = sizeof($list);
            $data["total"]   = $count;
            $data["msg"]     = "";
            $data["code"]    = 0;
            $data["data"]    = $list;
            $rs              = json($data);
            return $rs;
        }
    }


    //显示已完成的预约
    public function completejson()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $page            = input("get.page") ? input("get.page") : 1;
            $page            = intval($page);
            $limit           = input("get.limit") ? input("get.limit") : 1;
            $limit           = intval($limit);
            $start           = $limit * ($page - 1);
            $where['ename']  = Session::get('expertname');
            $where['status'] = 2;
            $list            = ordermodel::where($where)->limit($start, $limit)->select();
            $count           = sizeof($list);
            $data["total"]   = $count;
            $data["msg"]     = "";
            $data["code"]    = 0;
            $data["data"]    = $list;
            $rs              = json($data);
            return $rs;
        }
    }


    //教师修改预约
    public function orderupdate()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $data    = Request::post();
                $time    = date('Y-m-d H:i:s', time());
                $newdata = [
                    'id'      => $data['id'],
                    'address' => $data['address'],
                    'time'    => $data['time'],
                ];
                $res     = ordermodel::update($newdata);

                $notifydata = [
                    "write"     => Session::get('expertname'),
                    "receive"   => $data['sname'],
                    "sstatus"   => 1,
                    "eopera"    => 1,
                    "time"      => $time,
                    "ordertime" => $data['time']
                ];
                $notify     = notifymodel::insert($notifydata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }


    //显示留言
    public function messagejson()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $page  = input("get.page") ? input("get.page") : 1;
            $page  = intval($page);
            $limit = input("get.limit") ? input("get.limit") : 1;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list          = messagemodel::where('towho', Session::get('expertname'))->limit($start, $limit)->select();
            $count         = sizeof($list);
            $data["total"] = $count;
            $data["msg"]   = "";
            $data["code"]  = 0;
            $data["data"]  = $list;
            $rs            = json($data);
            return $rs;
        }
    }


    //图片上传
    public function photo()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $file  = $this->request->file('file');
            $info  = $file->move('../public/photo');
            $spath = expertmodel::where('id', Session::get('expertid'))->value('photo');
//        $explodepath = array();
//        $explodepath = explode("\\", $spath);
//        $rootpath    = Env::get('ROOT_PATH');
//        $oldpath     = $rootpath . 'public\photo\\' . $explodepath[3] . '\\' . $explodepath[4];
            if ($info) {
                $path    = '..\..\photo\\' . $info->getSaveName();
                $newdata = [
                    'id'    => Session::get('expertid'),
                    'photo' => $path,
                ];
                $res     = expertmodel::update($newdata);
//            unlink($oldpath);
                if ($res) {
                    return json(array('code' => 1, 'msg' => '成功', 'src' => $path));
                } else {
                    return json(array('code' => 0, 'msg' => '失败', 'src' => $path));
                }
            } else {
                return json(array('code' => 0, 'msg' => '失败', 'src' => '错误'));
            }
        }

    }

    //修改
    public function update()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            if (Request::isAjax()) {
                $data = Request::post();
                //salt化
                $password = $data['pwd'];
                $mpwd     = md5($password);
                $mspwd    = $this->salt($password);  //执行MD5散列
                $newdata  = [
                    'id'      => $data['id'],
                    'name'    => $data['name'],
                    'pwd'     => $mpwd,
                    'salt'    => $mspwd,
                    'sex'     => $data['sex'],
                    'mail'    => $data['mail'],
                    'age'     => $data['age'],
                    'level'   => $data['level'],
                    'address' => $data['address'],
                    'phone'   => $data['phone'],
                    'person'  => $data['person'],
                ];
                $res      = expertmodel::update($newdata);
                if ($res) {
                    $this->success('', '', '1');
                } else {
                    $this->error('', '', '0');
                }
            }
        }
    }


    //关于我们
    public function aboutus()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("expert/aboutus");
        }
    }


    //隐私条款
    public function yinsi()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("expert/yinsi");
        }
    }


    //联系我们
    public function contactus()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            $where['receive'] = Session::get('expertname');
            $where['estatus'] = 1;
            $list             = notifymodel::where($where)->select();
            $count            = sizeof($list);
            $this->assign('count', $count);
            return $this->fetch("expert/contactus");
        }
    }


    //退出
    function logout()
    {
        if ($this->logincheck() == false) {
            return $this->fetch("alert/alert");
        } else {
            Session::delete('expertname');
            Session::delete('expertid');
            return $this->fetch('login/login');
        }
    }

}